import { defineConfig } from 'vitepress'

import generateMenu from './theme/utils/generateMenu'
import { formatPages } from './theme/utils/formatPages'
import { genAside } from './theme/utils/genAside'

// java目录
let filesJava = await generateMenu('java')
filesJava = filesJava.filter((item) => /^[A-Z](\d+)-/i.test(item))
// filesJava = filesJava.filter((item) => !/-API\.md$/i.test(item))
const javaPages = formatPages(filesJava, 'java')

// web目录
let filesWeb = await generateMenu('web')
filesWeb = filesWeb.filter((item) => /^[A-Z](\d+)-/i.test(item))
// filesWeb = filesWeb.filter((item) => !/-API\.md$/i.test(item))
const webPages = formatPages(filesWeb, 'web')

// summary目录
let filesSummary = await generateMenu('summary')
filesSummary = filesSummary.filter((item) => /^[A-Z](\d+)-/i.test(item))
// filesSummary = filesSummary.filter((item) => !/-API\.md$/i.test(item))
const summaryPages = formatPages(filesSummary, 'summary')

// https://vitepress.dev/reference/site-config
export default defineConfig({
  title: 'Mr笔记', // 标签页的标题
  description: 'Mr的笔记站点', // 用于SEO
  head: [['link', { rel: 'icon', href: '/favicon.ico' }]],
  markdown: {
    lineNumbers: true, // 代码块显示行号啊
    toc: {
      level: [2, 3, 4, 5, 6, 7, 8]
    },
    image: {
      // 默认禁用图片懒加载
      lazyLoading: true
    },
    config: (md) => {
      // 替换 ==text== 为 <mark>text</mark>
      md.inline.ruler.before('emphasis', 'highlight', (state, silent) => {
        if (silent) return false

        const start = state.pos
        const max = state.posMax

        if (state.src.charCodeAt(start) !== 0x3d /* = */) return false
        if (start + 1 >= max) return false
        if (state.src.charCodeAt(start + 1) !== 0x3d /* = */) return false

        // 查找结束的 ==
        let pos = start + 2
        while (pos < max) {
          if (state.src.charCodeAt(pos) === 0x3d /* = */) {
            if (pos + 1 < max && state.src.charCodeAt(pos + 1) === 0x3d /* = */) {
              // 找到结束标记
              if (!silent) {
                const token = state.push('highlight', '', 0)
                token.content = state.src.slice(start + 2, pos)
                token.markup = '=='
              }
              state.pos = pos + 2
              return true
            }
          }
          pos++
        }

        return false
      })

      md.renderer.rules.highlight = (tokens, idx) => {
        return `<mark>${md.utils.escapeHtml(tokens[idx].content)}</mark>`
      }
    }
    // BUG: 按照官方文档配置后报错：Can't find handler for document
    // math: true
  },
  // 主题配置
  themeConfig: {
    // https://vitepress.dev/reference/default-theme-config
    nav: [
      { text: 'Java', link: '/doc/java' },
      { text: 'Web', link: '/doc/web' },
      { text: 'Web 参考', link: '/doc/web/API' },
      { text: '前端常用', link: '/doc/summary' }
    ],

    sidebar: {
      '/doc/java/': genAside(javaPages),
      '/doc/web/': genAside(webPages),
      '/doc/summary/': genAside(summaryPages)
    },
    // 大纲显示深度
    outline: {
      level: 'deep'
    },
    // 开启搜索
    search: {
      provider: 'local'
    },

    socialLinks: [{ icon: 'github', link: 'https://github.com/vuejs/vitepress' }]
  },
  vite: {
    build: {
      sourcemap: false,
      chunkSizeWarningLimit: 1000
    }
  }
})
